package com.guldan.jaina.utils;

import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.projectextensiondetail.ProjectExtDataSampleDTO;
import com.guldan.jaina.dto.projectextensiondetail.ProjectExtensionDetailDTO;
import com.guldan.jaina.dto.registerfail.RegisterFailDTO;
import com.guldan.mapper.db.entity.RegisterFail;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Silas.
 * Date: 2016/12/3
 * Time: 16:28
 */
public class RegisterFailDomainConvertUtils {


    public static RegisterFailDTO convert(RegisterFail entity) {
        RegisterFailDTO dto = new RegisterFailDTO();

        dto.setRegisterId(entity.getRegisterId());
        dto.setUploadId(entity.getUploadId());
        dto.setBatchId(entity.getBatchId());
        dto.setCreatorAreaId(entity.getCreatorAreaId());
        dto.setCitizenInfoId(entity.getCitizenInfoId());
        dto.setProjectId(entity.getProjectId());
        dto.setProjectName(entity.getProjectName());
        dto.setYear(entity.getYear());
        dto.setSubsidyCount(entity.getSubsidyCount());
        dto.setRegisterAreaName(entity.getRegisterAreaName());
        dto.setName(entity.getName());
        dto.setIdentNumber(entity.getIdentNumber());
        dto.setBankName(entity.getBankName());
        dto.setBankNumber(entity.getBankNumber());
        dto.setSubsidyMoney(entity.getSubsidyMoney());
        dto.setExtData(entity.getExtData());

        if (StringUtils.isNotBlank(entity.getExtData())) {
            List<ProjectExtDataSampleDTO> pedList = new ArrayList<>();
            JSONArray jsonArray = JSONArray.fromObject(entity.getExtData());
            for (int i = 0; i < jsonArray.size(); i++) {
                ProjectExtDataSampleDTO dataSampleDTO = new ProjectExtDataSampleDTO();
                dataSampleDTO.setExtId(jsonArray.getJSONObject(i).getInt("extId"));
                dataSampleDTO.setExtName(jsonArray.getJSONObject(i).getString("extName"));
                dataSampleDTO.setExtContent(jsonArray.getJSONObject(i).getString("extContent"));
                pedList.add(dataSampleDTO);
            }
            dto.setExtDataList(pedList);
        }
        dto.setFailReason(entity.getFailReason());

        dto.setCreateTime(DateUtil.toLocaleString(entity.getCreateTime(), DateUtil.YYYY_MM_DD_HH_DD_SS));
        dto.setCreator(entity.getCreator());
        dto.setDelete(entity.getIsDelete());
        dto.setDisabled(entity.getDisabled());
        dto.setModifier(entity.getModifier());
        dto.setModifyTime(DateUtil.toLocaleString(entity.getModifyTime(), DateUtil.YYYY_MM_DD_HH_DD_SS));

        return dto;
    }
}

